package com.example.layuitest.controller;

import com.example.layuitest.entity.Article;
import com.example.layuitest.entity.Customer;
import com.example.layuitest.entity.Pokemon;
import com.example.layuitest.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller

public class MyController {
    @Autowired
    MyService myService;
    @GetMapping("/test")
    public String test(){
        return "test";
    }
    @GetMapping("/")
    public String registerPage(){
        return "register";
    }
    @PostMapping("/register")
    @ResponseBody
    public Map<String,String> register(Customer customer){
        System.out.println(customer);
        Map<String,String> map = new HashMap<>();
        map.put("msg","注册成功");
        return map;
    }
    @GetMapping("/pokemon")
    public String pokemon(){
        return "pokemon";
    }
    @GetMapping("/pokemon1")
    @ResponseBody
    public Map<String, Pokemon> pokemon1(){
        Map<String,Pokemon> map = new HashMap<>();
        Pokemon pokemon = myService.getCount();
        map.put("pokemon",pokemon);
        return map;
    }
    @GetMapping("/list")
    public String list(){
        return "list";
    }
    @GetMapping("/getList")
    @ResponseBody
    public Map<String, Object> getList(int page, int limit){
        if (page == 0){
            page = 1;
        }
        int start = (page - 1) * limit;
        Map<String, Integer> param = new HashMap<>();
        param.put("start", start);
        param.put("limit", limit);
        Map<String,Object> map = new HashMap<>();
        List<Article> articleList = myService.getArticlePage(param);
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",myService.getArticle());
        map.put("data",articleList);
        return map;
    }
    @PostMapping("/getArticleByid/{id}")
    @ResponseBody
    public Map<String, Object> getArticleByID(@PathVariable("id") int id){
        Map<String,Object> map = new HashMap<>();
        Article article = myService.getArticleByid(id);
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",1);
        map.put("data",article);
        return map;
    }
    @PostMapping("/updateArticle")
    @ResponseBody
    public Map<String, Object> updateArticle(Article article){
        Map<String,Object> map = new HashMap<>();
        Integer re = myService.updateArticle(article);
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",1);
        map.put("data",re);
        return map;
    }
    @PostMapping("/deleteArticleId")
    @ResponseBody
    public Map<String, Object> deleteArticleId(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        Integer re = null;
        for (Integer i : ids) {
            re = myService.deleteArticleIds(i);
        }
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",1);
        map.put("data",re);
        return map;
    }
}
